/*
 * svmRegression.java
 *
 * Created on October 13, 2004, 12:38 AM
 */

package portfolio.stock.analize;

import java.util.Vector;
import javax.swing.*;
import libsvm.*;


/**
 *
 * @author  ACA
 */
public class SvmRegression //extends JInternalFrame
{    
    private svm_problem   prob;		// set by read_problem
    private svm_parameter param;        // set by parse_command_line
    private String        error_msg;
    private svm_model     model;   
       
    private double []     dYTarget = null;
    private double [][]   dXMatrix = null;
    private int           iColumn  = 0;
    private int           iRow     = 0;
    /****************************************************/
    public SvmRegression(
                         svm_parameter svmParam,
                         double []     dYParam, 
                         double [][]   dXParam
                        )
    {   
        param     = svmParam;
        dYTarget  = dYParam;
        dXMatrix  = dXParam;
        
    } /* End Constructor selectSvmAttributes */    
    /*********************************************************/
    public void readSvmProblem ()
    {
        Vector vy     = new Vector();
        Vector vx     = new Vector();
        int iRow    = dXMatrix.length;
        int iColumn = dXMatrix [0].length;
        int max_index = 0;

        //svm_node[] x = new svm_node[m];
        for (int i = 0; i < iRow; i++) {
            svm_node[] x = new svm_node[iColumn];
            vy.addElement (new Double (dYTarget [i]));
            for (int j = 0; j < iColumn; j++) {
                x[j] = new svm_node();
                x[j].index = j; //ACA atoi(st.nextToken());
                x[j].value = dXMatrix [i][j];
                //System.out.print (x[j].value +" ");
            } 
            if(iColumn > 0) 
                max_index = Math.max (max_index, x[iColumn-1].index);
            vx.addElement(x);  

            //System.out.println ();
        }

        prob   = new svm_problem();
        prob.l = vy.size();

        prob.x = new svm_node [prob.l][];
        for (int i=0; i < prob.l; i++) {
                prob.x[i] = (svm_node[]) vx.elementAt(i);
        }

        prob.y = new double [prob.l];
        for(int i=0; i < prob.l ;i++) {
                prob.y [i] = ((Double)vy.elementAt(i)).doubleValue();
                //System.out.println (prob.y[i]);
        }
        if (param.gamma == 0)
            param.gamma = 1.0 / max_index;

        return;
    } /* End Method*/    
   /*********************************************************/
    public double [] svmPredict (double [][] dXExpMatrix)
    {
        int    correct = 0;
        int    total   = 0;
        int    error   = 0;
        double sumv    = 0;
        double sumy    = 0;
        double sumvv   = 0;
        double sumyy   = 0;
        double sumvy   = 0;
        double [] dTimeSeries = null;
        
        int iRow    = dXExpMatrix.length;
        int iColumn = dXExpMatrix [0].length;
        
        if (dXExpMatrix == null) {
            return null;
        }

        try {
            //DataOutputStream output = new DataOutputStream(new FileOutputStream("svm_output.txt"));

            dTimeSeries = new double [iRow]; //[2];
//System.out.println ("iRow " + iRow + " dYTarget " + dYTarget.length);
            for (int i = 0; i < iRow; i++) {
                svm_node[] x = new svm_node[iColumn];
                //double dTarget = dYTarget [i]; //<<<<<<<<<<< 0j0 getTableDouble (2, i);
                //System.out.print (i + "::");
                for (int j = 0; j < iColumn; j++) {
                    double dTemp = dXExpMatrix [i][j]; //<<<<<<<<<< 0j0 getTableDouble (j +3, i);
                    //System.out.print (dTemp+" ");
                    x[j]       = new svm_node();
                    x[j].index = j; //ACA atoi(st.nextToken());
                    x[j].value = dTemp;
                } 
                double v;
                v = svm.svm_predict(model, x);

                //dTimeSeries [i][0] = dTarget;
                dTimeSeries [i] = v;

                //output.writeBytes(v+"\n");
                //if(v == dTarget)
                //        ++correct;
 
                //ACAerror += (v - dTarget) * (v - dTarget);
                //ACAsumv  += v;
                //ACAsumy  += dTarget;
                //ACAsumvv += v*v;
                //ACAsumyy += dTarget * dTarget;
                //ACAsumvy += v * dTarget;
                //++total;

                //System.out.println ();
            }
        }
        catch (Exception ex) {
            JOptionPane.showMessageDialog (null, "Error: svmPredict");
        };
//        System.out.print("Accuracy = "+(double)correct/total*100+
//                         "% ("+correct+"/"+total+") (classification)\n");
//        double dMeanSquaredError = (total != 0)? error/total: 999999.999;
//        System.out.print("Mean squared error = "+ dMeanSquaredError +" (regression)\n");
//        System.out.print("Squared correlation coefficient = "+
//                ((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
//                ((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))+" (regression)\n"
//                );

        return (dTimeSeries);
    } /* End Method svmPredict */    
    /////////////////////////
    public String svmCheckParameter ()
    {
        if (prob == null)
            return null;
        if (param == null)
            return null;

        return svm.svm_check_parameter (prob, param);
    }
    public void svmTrain ()
    {
        model = svm.svm_train (prob, param);
        return;
    }
    public int svmGetNrClass ()
    {
        if (model == null)
            return 0;

        return svm.svm_get_svm_type (model);
    }
} /* End class selectSvmRegression */
